<?php
namespace app\vote\controller;
use think\Session;
class Root extends \think\Controller
{
    public function _initialize(){
        $user=db('user')->count(); //观众人数
       $player=db('player')->count();//选手人数
       $race=db('race')->count();//赛事数
       $pw=db('judges')->count();//评委数
       $racemess=db('race')->paginate(4);
       $id=Session::get('user');
       $result=db('root')->where('rootid',$id)->find();
        $this->assign(['user'=>$user,'player'=>$player,'race'=>$race,'pw'=>$pw,'racemess'=>$racemess,'name'=>$result['rootname']]);
    }
    public function home(){  
        return view();
    }

    //退出登录
    public function outlogin(){
        Session::delete('user');
        Session::delete('password');
        
        if(!Session::has('user') and !Session::has('password')){
            $this->redirect('RootLogin/Login');
        }
    }

    //查询
    public function find(){
        $request=request()->param();
        $sid=$request['sid'];
        $table=$request['table'];
        if($table=='judges'){
            $result=db("$table")->join('race','judges.sid=race.rid','LEFT')->where('sid',"$sid")->paginate(4);
            $race=db('race')->select();
            return view('pw',['data'=>$result,'race'=>$race]);
        }elseif($table=='player'){
            $result=db("$table")->join('race','player.sid=race.rid','LEFT')->where('sid',"$sid")->paginate(4);
            $race=db('race')->select();
            return view('player',['data'=>$result,'race'=>$race]);
        }
    }
        
    public function search(){
        $request=request()->param();
        $data=$request['mess'];
        $table=$request['table'];
        if($table=='judges'){
            $result=db("$table")->join('race','judges.sid=race.rid','LEFT')->where('jname','like',"%$data%")->paginate(4);
            $race=db('race')->select();
            return view('pw',['data'=>$result,'race'=>$race]);
        }
        if($table=='race'){
            $result=db("$table")->where('rname','like',"%$data%")->paginate(4);
            return view('race',['data'=>$result]);
        }
        if($table=='player'){
            $result=db("$table")->join('race','player.sid=race.rid','LEFT')->where('pname','like',"%$data%")->paginate(4);
            $race=db('race')->select();
            return view('player',['data'=>$result,'race'=>$race]);
        }
        if($table=='user'){
            $result=db("$table")->where('username','like',"%$data%")->paginate(4);
            return view('user',['data'=>$result]);
        }
    }

//批量导入数据
    public function excel(){
         //获取上传文件信息
         $file = request()->file('file');

         //1000b=1kb  大小不超过10mb
         $info = $file->validate(['size'=>10000000 ,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
         if($info){
                 //获取文件名
                 $excel_path=$info->getSaveName(); //'20190218\xinxi.xlsx'
                 //获取上传文件地址--
                 $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $excel_path;//D:\wamp\www\admin1\public\excel\20190218\xinxi.xlsx
                 //获取上传文件后缀
                 $name = $info->getExtension();
                 //引入类
                 vendor("PHPExcel.PHPExcel.IOFactory");
                 if($name =='xlsx' ){
                     $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
                 }else{
                     $objReader = \PHPExcel_IOFactory::createReader('Excel5'); 
                 }
                 //只读去数据，忽略里面各种格式
                 $objReader->setReadDataOnly(true);
                 //加载文件内容 utf8geshi
                 $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');
                 // echo "<pre>";
                 //转换为数组格式
                 $excel_array=$obj_PHPExcel->getsheet(0)->toArray();
                 
                 //输出数组中的当前元素和下一个元素的值，然后把数组的内部指针重置到数组中的第一个元素
                 $arr  = reset($excel_array);
                 //销毁标题
                 unset($excel_array[0]); 
                 $data = [];  
                 $i=0;  
                 foreach ($excel_array as $k => $v) {
                     $data[$k]['pname'] = $v[0]; 
                     $data[$k]['sex'] = $v[1]; 
                     $data[$k]['work'] = $v[2]; 
                     $data[$k]['introduce'] = $v[3]; 
                     $data[$k]['ppic'] =$v[4]; 
                     $data[$k]['score'] = $v[5]; 
                     $data[$k]['sid'] = $v[6]; 
                     $i++; 
                 }
                //  dump($data);exit;
                 $res = db('player')->insertAll($data);
                 if($res){
                     $this->success('添加成功','root/player');
                 }else{
                     $this->error('添加失败','root/player');
                 }
             }else{
                 $this->error($file->getError());
             }
    }


//比赛管理
    public function race(){
        $result=db('race')->paginate(4);
        return view('',['data'=>$result]);
    }

    public function delrace(){ //删除赛事
        $request=request()->param();
        $id=$request['id'];
        $result=db('race')->where('rid',$id)->delete();
        if($result){
            $this->redirect('Root/race');
        }else{
            $this->redirect('Root/race');
        }
    }

    public function editrace(){
        $request=request()->param();
        $id=$request['id'];
        $result=db('race')->where('rid',$id)->find();
        $this->assign('data',$result);
        return $this->fetch('editrace');
    }

    public function doraceedit(){
        $request=request()->param();
        $id=$request['id'];
        $name=$request['name'];
        $explain=$request['explain'];
        $start=$request['start'];
        $end=$request['end'];
        $file = request()->file('pic');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->rule('uniqid')->move('./static/vote/pics');
            if($info){
                // 成功上传后 获取上传信息
                $path="/pics/".$info->getFilename();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }elseif(empty($file)){//如果没有传,图片还是原来的路径
            $path=$request['oldpic'];
        }
        $data=['rname'=>$name,'explain'=>$explain,'start'=>$start,'end'=>$end,'rpic'=>$path];
        $result=db('race')->where('rid',$id)->update($data);
        $this->redirect('root/race');
    }

    public function addrace(){
        return view();
    }
    public function doaddrace(){
        $request=request()->param();
              // 获取表单上传文件 
            $file = request()->file('pic');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $info = $file->rule('uniqid')->move('./static/vote/pics');
                if($info){
                    // 成功上传后 获取上传信息
                 
                    $path="/pics/".$info->getFilename();
                    $name=$request['name'];
                    $explain=$request['explain'];
                    $start=$request['start'];
                    $end=$request['end'];
                    $data=['rname'=>$name,'explain'=>$explain,'start'=>$start,'end'=>$end,'rpic'=>$path];

                    $reuslt=db('race')->insert($data);
                    if($reuslt){
                        return $this->redirect('Root/race');
                    }else{
                        $this->error('添加失败','Root/race');
                    }

                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
    }


    //评委管理
        public function pw(){
            $result=db('judges')->join('race','judges.sid=race.rid','LEFT')->paginate(4);
            $race=db('race')->select();
            return view('',['data'=>$result,'race'=>$race]);
        }

        public function delpw(){
            $request=request()->param();
            $id=$request['id'];
            $result=db('judges')->where('jid',$id)->delete();
            if($result){
                $this->redirect('Root/pw');
            }else{
                $this->redirect('Root/pw');
            }
        }
        public function resetpw(){
            $request=request()->param();
            $id=$request['id'];
            $data=['password'=>'123456'];
            $result=db('judges')->where('jid',$id)->update($data);
            if($result){
                $this->redirect('Root/pw');
            }else{
                $this->redirect('Root/pw');
            }
        }

        public function editpw(){
            $request=request()->param();
            $id=$request['id'];
            $result=db('judges')->where('jid',$id)->find();
            $race=db('race')->select();
            $this->assign(['data'=>$result,'race'=>$race]);
            return $this->fetch('editpw');
        }

        public function doeditpw(){
            $request=request()->param();
            $id=$request['id'];
            $name=$request['name'];
            $tel=$request['tel'];
            $sid=$request['sid'];
            $file = request()->file('pic');
            // 移动到框架应用根目录/public/uploads/ 目录下
            if($file){
                $info = $file->rule('uniqid')->move('./static/vote/pics');
                if($info){
                    // 成功上传后 获取上传信息
                    $path="/pics/".$info->getFilename();
                }
                else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }elseif(empty($file)){//如果没有传,图片还是原来的路径
                $path=$request['oldpic'];
            }
            $data=['jname'=>$name,'tel'=>$tel,'sid'=>$sid,'jpic'=>$path];
            // var_dump($id);
            $result=db('judges')->where('jid',$id)->update($data);
            if($result){
                $this->redirect('Root/pw');
            }else{
                $this->redirect('Root/pw');

            }
            
        }

        public function addpw(){
            $race=db('race')->select();
            return view('',['race'=>$race]);
        }
        public function doaddpw(){
            $request=request()->param();
            // 获取表单上传文件 
          $file = request()->file('pic');
          // 移动到框架应用根目录/public/uploads/ 目录下
          if($file){
              $info = $file->rule('uniqid')->move('./static/vote/pics');
              if($info){
                  // 成功上传后 获取上传信息
               
                  $path="/pics/".$info->getFilename();
                  $name=$request['name'];
                  $tel=$request['tel'];
                  $pwd='123456';
                  $sid=$request['sid'];
                  $data=['jname'=>$name,'tel'=>$tel,'password'=>$pwd,'sid'=>$sid,'jpic'=>$path];

                  $reuslt=db('judges')->insert($data);
                  if($reuslt){
                      return $this->redirect('Root/pw');
                  }else{
                      $this->error('添加失败','Root/pw');
                  }

              }else{
                  // 上传失败获取错误信息
                  echo $file->getError();
              }
          }
        }

        //选手管理
       public function player(){
        $result=db('player')->join('race','player.sid=race.rid','LEFT')->paginate(4);
        $race=db('race')->select();
        return view('',['data'=>$result,'race'=>$race]);
       }
   
       public function delplayer(){
            $request=request()->param();
            $id=$request['id'];
            $result=db('player')->where('pid',$id)->delete();
            if($result){
                $this->redirect('Root/player');
            }else{
                $this->redirect('Root/player');
            }
       }
       public function editplayer(){
            $request=request()->param();
            $id=$request['id'];
            $result=db('player')->where('pid',$id)->find();
            $race=db('race')->select();
            $this->assign(['data'=>$result,'race'=>$race]);
            return $this->fetch('editplayer');
       }

       public function doeditplayer(){
        $request=request()->param();
        $id=$request['id'];
        $name=$request['name'];
        $sex=$request['sex'];
        $work=$request['work'];
        $introduce=$request['introduce'];
        $score=$request['score'];
        $sid=$request['sid'];
        $file = request()->file('pic');
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->rule('uniqid')->move('./static/vote/pics');
            if($info){
                // 成功上传后 获取上传信息
                $path="/pics/".$info->getFilename();
            }
            else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }elseif(empty($file)){//如果没有传,图片还是原来的路径
            $path=$request['oldpic'];
        }
        $data=['pname'=>$name,'sex'=>$sex,'work'=>$work,'introduce'=>$introduce,'sid'=>$sid,'score'=>$score,'ppic'=>$path];
        $result=db('player')->where('pid',$id)->update($data);
        if($result){
            $this->redirect('root/player');
        }else{
            $this->redirect('root/player');
        }
       }

       public function addplayer(){
        $race=db('race')->select();
        return view('',['race'=>$race]);
       }

       public function doaddplayer(){
        $request=request()->param();
        // 获取表单上传文件 
      $file = request()->file('pic');
      // 移动到框架应用根目录/public/uploads/ 目录下
      if($file){
          $info = $file->rule('uniqid')->move('./static/vote/pics');
          if($info){
              // 成功上传后 获取上传信息
           
              $path="/pics/".$info->getFilename();
              $name=$request['name'];
              $sex=$request['sex'];
              $work=$request['work'];
              $introduce=$request['introduce'];
              $sid=$request['sid'];
              $data=['pname'=>$name,'sex'=>$sex,'work'=>$work,'introduce'=>$introduce,'sid'=>$sid,'ppic'=>$path];

              $reuslt=db('player')->insert($data);
              if($reuslt){
                  return $this->redirect('Root/player');
              }else{
                  $this->error('添加失败','Root/player');
              }

          }else{
              // 上传失败获取错误信息
              echo $file->getError();
          }
        }
       }

    public function playergroup(){ //对选手分组进行随机分组处理,存入数据库的表playergroup中
      $request=request()->param();
      $sid=$request['id'];
      $allplayer=db('player')->where('sid',$sid)->select();
    //   var_dump($allplayer);
      shuffle($allplayer);
    //   echo "<br>";
    //   var_dump($allplayer);
     $lenth=ceil(count($allplayer)/2);
    //  echo $lenth;
     for($i=0;$i<$lenth;$i++){
        $allplayer[2*$i]['group']=$i+1;
        $allplayer[2*$i+1]['group']=$i+1;
     }
     foreach($allplayer as $key=>$value){
        if(count($value) == "1"){
            unset($allplayer[$key]);
        }
     }
     for($j=0;$j<count($allplayer);$j++){
        $pid=$allplayer[$j]['pid'];
        $pname=$allplayer[$j]['pname'];
        $sid=$allplayer[$j]['sid'];
        $group=$allplayer[$j]['group'];
        $data=["pid"=>$pid,"pname"=>$pname,"rid"=>$sid,"group"=>$group];
        db('playergroup')->insert($data);
     }
     $this->redirect('Root/race');
    //  var_dump($allplayer);
    }

    public function showgroup(){
        $result=db('player')->join('race','player.sid=race.rid','LEFT')->join('playergroup','player.pid=playergroup.pid')->paginate(4);
        $race=db('race')->select();
        return view('',['data'=>$result,'race'=>$race]);

    }


       //观众管理
       public function user(){
        $result=db('user')->paginate(4);
        return view('',['data'=>$result]);
       }

       public function deluser(){
            $request=request()->param();
            $id=$request['id'];
            $result=db('user')->where('userid',$id)->delete();
            if($result){
                $this->redirect('Root/user');
            }else{
                $this->redirect('Root/user');
            }
       }

       public function resetuser(){
            $request=request()->param();
            $id=$request['id'];
            $data=['password'=>'123456'];
            $result=db('user')->where('userid',$id)->update($data);
            if($result){
                $this->redirect('Root/user');
            }else{
                $this->redirect('Root/user');
            }
       }

       public function person(){
        
            $id=Session::get('user');
            $result=db('root')->where('rootid',$id)->find();
            return view('',['data'=>$result]);
       }
       public function personedit(){
            $request=request()->param();
            $id=$request['id'];
            $name=$request['name'];
            $password=$request['password'];
            $phone=$request['phone'];
            $data=['rootname'=>$name,'password'=>$password,'tel'=>$phone];
            // var_dump($id);
            $result=db('root')->where('rootid',$id)->update($data);
            if($result){
                $this->redirect('Root/person');
            }else{
                $this->redirect('Root/person');

            }

       }
}   


